class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

public class reverseList {
    // 迭代
    public ListNode _reverseList(ListNode head) {
        ListNode pre = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = pre;
            pre.next = null;
            pre = curr;
            curr = next;
        }
        return pre;
    }

    // 递归
    public ListNode _reverseList2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode node = _reverseList2(head.next);
        head.next.next = head;
        head.next = null;
        return node;
    }

}
